package com.jzo2o.health.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.health.model.excel.ReservationImportData;
import com.jzo2o.health.service.IReservationSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * 预约设置操作
 * @author itcast
 */
@Slf4j
@RestController("adminReservationBatchSettingController")
@RequestMapping("/admin/reservation-setting")
@Api(tags = "管理端 - 批量预约设置相关接口")
public class ReservationBatchSettingController {
    @Resource
    private IReservationSettingService reservationSettingService;

    @PostMapping("/upload")
    @ApiOperation("上传文件批量预约设置")
    public void upload(@RequestPart("file") MultipartFile file) {
        List<ReservationImportData> reservationDataList;

        try (InputStream inputStream = file.getInputStream()) {
            reservationDataList = EasyExcel.read(inputStream)
                    .head(ReservationImportData.class)
                    .sheet("预约设置模板")
                    .doReadSync();
        } catch (Exception e) {
            // 防止过度包装
            throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
        }

        if (CollUtils.isNotEmpty(reservationDataList)) {
            reservationSettingService.editNumberByDateBatch(reservationDataList);
        }
    }
}